package com.lsh.payment.core.handler.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.lsh.payment.api.model.basevo.BaseResponse;
import com.lsh.payment.api.model.basevo.ExceptionStatus;
import com.lsh.payment.api.model.refund.RefundResponse;
import com.lsh.payment.core.handler.AbstractRefundChannelHandler;
import com.lsh.payment.core.model.payenum.RefundStatus;
import com.lsh.payment.core.model.payment.PayDeal;
import com.lsh.payment.core.model.refund.PayRefund;
import com.lsh.payment.core.service.alipay.IAliRefundService;
import com.lsh.payment.core.service.refund.IRefundOrderStatusService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Project Name: lsh-payment
 * Created by peter on 18/6/1.
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
@Slf4j
public class AliRefundHandler extends AbstractRefundChannelHandler {

    @Autowired
    private IAliRefundService aliRefundService;

    @Override
    public RefundResponse refund(PayRefund payRefund, PayDeal payDeal) {
        String logPrefix = "【支付宝退款】";

        RefundResponse refundResponse = new RefundResponse();
        AlipayTradeRefundResponse response = aliRefundService.refund(payRefund, payDeal);

        if (response.isSuccess()) {

            log.info("【支付宝退款查询结果】 {}", JSON.toJSONString(response));

            IRefundOrderStatusService refundOrderStatusService = this.getRefundOrderStatusService(RefundStatus.REFUND_ING);

            if (refundOrderStatusService.modifyStatus(payRefund, true)) {
                JSONObject content = new JSONObject();
                content.put("payment_refund_no", payRefund.getPaymentRefundNo());
                refundResponse.setContent(content);
            }

            refundResponse.setRet(Integer.parseInt(ExceptionStatus.SUCCESS.getCode()));
            refundResponse.setMsg(ExceptionStatus.SUCCESS.getMessage());

        } else {
            log.info("{} 返回失败", logPrefix);
            log.info("sub_code:{},sub_msg:{}", response.getSubCode(), response.getSubMsg());

            String ext = payRefund.getExt();
            JSONObject extJson;
            if (StringUtils.isNotBlank(ext)) {
                extJson = JSON.parseObject(ext);
            } else {
                extJson = new JSONObject();
            }
            extJson.put("err_code", response.getSubCode());
            extJson.put("err_code_des", response.getSubMsg());
            payRefund.setExt(extJson.toJSONString());
            this.getRefundOrderStatusService(RefundStatus.REFUND_EXCEPTION).modifyStatus(payRefund, false);

            refundResponse.setRet(2001005);
            refundResponse.setMsg("退款申请失败," + response.getSubCode() + "," + response.getSubMsg());
        }

        return refundResponse;
    }


    @Override
    public BaseResponse refundQuery(PayRefund payRefund, PayDeal payDeal) {
        String logPrefix = "【支付宝退款查询】";
        BaseResponse baseResponse = new BaseResponse();

        if (payRefund.getRefundStatus() == RefundStatus.REFUND_ING.getValue()) {
            AlipayTradeFastpayRefundQueryResponse response = aliRefundService.refundQuery(payRefund, payDeal);
            log.info("{} 结果 {}", logPrefix, JSON.toJSONString(response));
            if (response.isSuccess()) {
//                String refundStatus = response.getRefundStatus();
//                log.info("{} 结果 {}", logPrefix, JSON.toJSONString(response));
//                if (refundStatus.equals(AliRefundStatus.ALI_REFUND_SUCCESS)) {
                    payRefund.setChannelRefundId(response.getOutRequestNo());
                    if (this.getRefundOrderStatusService(RefundStatus.REFUND_SUCCESS).modifyStatus(payRefund, true)) {
                        baseResponse.setRet(0);
                        baseResponse.setMsg("支付宝退款成功");
                    }

                    return baseResponse;
//                } else if (refundStatus.equals(AliRefundStatus.ALI_REFUND_FAIL)) {
//                    String ext = payRefund.getExt();
//                    JSONObject extJson;
//                    if (StringUtils.isNotBlank(ext)) {
//                        extJson = JSON.parseObject(ext);
//                    } else {
//                        extJson = new JSONObject();
//                    }
//                    extJson.put("err_code", response.getErrorCode());
//                    extJson.put("err_code_des", response.getErrorCode());
//                    payRefund.setExt(extJson.toJSONString());
//                    if (this.getRefundOrderStatusService(RefundStatus.REFUND_EXCEPTION).modifyStatus(payRefund, true)) {
//                        baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2006002.getCode()));
//                        baseResponse.setMsg("支付宝退款失败");
//                    }
//
//                    return baseResponse;
//                } else {
//                    baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2000000.getCode()));
//                    baseResponse.setMsg("支付宝退款进行中");
//                }
//
//                return baseResponse;
            } else {
                String ext = payRefund.getExt();
                JSONObject extJson;
                if (StringUtils.isNotBlank(ext)) {
                    extJson = JSON.parseObject(ext);
                } else {
                    extJson = new JSONObject();
                }
                extJson.put("err_code", response.getErrorCode());
                extJson.put("err_code_des", response.getErrorCode());
                payRefund.setExt(extJson.toJSONString());
                if (this.getRefundOrderStatusService(RefundStatus.REFUND_EXCEPTION).modifyStatus(payRefund, true)) {
                    baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2006003.getCode()));
                    baseResponse.setMsg("退款申请成功，查询退款结果FAIL,请联系技术人员");
                }

//                baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2006003.getCode()));
//                baseResponse.setMsg("退款申请成功，查询退款结果FAIL,请联系 技术人员");
            }

        } else if (payRefund.getRefundStatus() == RefundStatus.REFUND_SUCCESS.getValue()) {
            baseResponse.setRet(0);
            baseResponse.setMsg("支付宝退款成功");
        } else if (payRefund.getRefundStatus() == RefundStatus.REFUND_NEW.getValue()) {
            baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2000000.getCode()));
            baseResponse.setMsg("支付宝退款进行中");
        } else if (payRefund.getRefundStatus() == RefundStatus.REFUND_APPLY.getValue()) {
            baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2000000.getCode()));
            baseResponse.setMsg("支付宝退款进行中");
        } else if (payRefund.getRefundStatus() == RefundStatus.REFUND_CLOSE.getValue()) {
            baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2006001.getCode()));
            baseResponse.setMsg(ExceptionStatus.E2006001.getMessage());
        } else {
            baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2006002.getCode()));
            baseResponse.setMsg("支付宝退款失败");
        }

        return baseResponse;
    }


}
